﻿namespace EyeSoft.Test.Security.Cryptography
{
	using System.IO;
	using System.Linq;

	using EyeSoft.Extensions;
	using EyeSoft.Security.Cryptography;

	using Microsoft.VisualStudio.TestTools.UnitTesting;

	using SharpTestsEx;

	[TestClass]
	public class Sha1ExtensionsTest
	{
		private const string Value = "TestString";
		private const string KnownSha1 = "d598b03bee8866ae03b54cb6912efdfef107fd6d";

		private readonly byte[] buffer =
			Value.ToCharArray().Select(c => (byte)c).ToArray();

		[TestMethod]
		public void CalculateSha1OnString()
		{
			Value
				.ComputeSha1()
				.Should()
				.Be.EqualTo(KnownSha1);
		}

		[TestMethod]
		public void CalculateSha1OnStream()
		{
			new MemoryStream(buffer)
				.Using(stream => stream.ComputeSha1().Should().Be.EqualTo(KnownSha1));
		}

		[TestMethod]
		public void CalculateSha1OnStreamAtEndThrowException()
		{
			new MemoryStream(buffer)
				.Using(
					stream =>
						{
							stream.Position = buffer.Length;

							Executing
								.This(() => stream.ComputeSha1())
								.Should()
								.Throw<EndOfStreamException>();
						});
		}
	}
}